package com.hhit.zhgd.controller

import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.hhit.zhgd.domain.dto.BackStageMaterialsSupplyPlanDTO
import com.hhit.zhgd.domain.dto.CakeInfo
import com.hhit.zhgd.domain.entity.ZhgdMaterialsSupplyPlan
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdMaterialsSupplyPlanService
import jakarta.annotation.Resource
import jakarta.servlet.http.HttpServletResponse
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import javax.validation.Valid

@RestController
class ZhgdMaterialsSupplyPlanController {
    @Resource
    private val zhgdMaterialsSupplyPlanService: ZhgdMaterialsSupplyPlanService? = null

    /**
     * @描述 导入物资供应计划
     * @参数 [multipartFile]
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/7/17
     * @修改人和其它信息
     */
    @PostMapping("backStage/importMaterialsSupplyPlan")
    @Throws(Exception::class)
    fun importMaterialsSupplyPlan(@RequestParam("file") multipartFile: MultipartFile?): BaseResp<Any?>? {
        return zhgdMaterialsSupplyPlanService?.importMaterialsSupplyPlan(multipartFile)
    }

    /**
     * @描述 分页查询 物资供应计划
     * @参数 [description, determineDeliveryDate, page, size]
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/7/17
     * @修改人和其它信息
     */
    @GetMapping("getMaterialsSupplyPlans")
    fun getMaterialsSupplyPlans(
        @RequestParam(name = "description", defaultValue = "") description: String?,
        @RequestParam(name = "determineDeliveryDate", defaultValue = "") determineDeliveryDate: String?,
        @RequestParam(name = "page", defaultValue = "1") page: Int,
        @RequestParam(name = "size", defaultValue = "15") size: Int
    ): BaseResp<Any?>? {
        val pageInfo = Page<ZhgdMaterialsSupplyPlan>(page.toLong(), size.toLong())
        val queryWrapper =
            QueryWrapper<ZhgdMaterialsSupplyPlan>().like(StrUtil.isNotBlank(description), "description", description)
                .or().lt(StrUtil.isNotBlank(determineDeliveryDate), "determine_delivery_date", determineDeliveryDate)
        return BaseResp.success(zhgdMaterialsSupplyPlanService?.page(pageInfo, queryWrapper))
    }

    /**
     * @描述 修改交货状态
     * @参数 [id]
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/7/24
     * @修改人和其它信息
     */
    @GetMapping("confirmHandOverMaterials")
    fun confirmHandOverMaterials(@RequestParam("id") id: Int?): BaseResp<Any?>? {
        return zhgdMaterialsSupplyPlanService?.confirmHandOverMaterials(id)
    }


    @PostMapping("addMaterialPlan")
    fun add(@RequestBody info: ZhgdMaterialsSupplyPlan?): BaseResp<Any?>? {
        zhgdMaterialsSupplyPlanService?.save(info)
        return BaseResp.success()
    }

    /**
     * @description 后台管理-进度管理-添加物资供应计划
     * @author qianchengcheng
     * @date 2024/6/28 13:53
     */
    @PostMapping("backStage/addMaterialPlan")
    fun addMaterialPlan(
        @RequestBody backStageMaterialsSupplyPlanDTO: @Valid BackStageMaterialsSupplyPlanDTO?,
        bindingResult: BindingResult
    ): BaseResp<Any?>? {
        if (bindingResult.hasErrors()) {
            val defaultMessage = bindingResult.fieldError?.defaultMessage
            return BaseResp.fail(defaultMessage)
        }
        return zhgdMaterialsSupplyPlanService?.addMaterialPlan(backStageMaterialsSupplyPlanDTO)
    }

    /**
     * @description 后台管理-进度管理-修改物资供应计划
     * @author qianchengcheng
     * @date 2024/6/28 15:13
     */
    @PostMapping("backStage/editMaterialPlan")
    fun editMaterialPlan(
        @RequestBody backStageMaterialsSupplyPlanDTO: @Valid BackStageMaterialsSupplyPlanDTO?,
        bindingResult: BindingResult
    ): BaseResp<Any?>? {
        if (bindingResult.hasErrors()) {
            val defaultMessage = bindingResult.fieldError?.defaultMessage
            return BaseResp.fail(defaultMessage)
        }
        return zhgdMaterialsSupplyPlanService?.editMaterialPlan(backStageMaterialsSupplyPlanDTO)
    }

    /**
     * @description 后台管理-进度管理-删除物资供应计划
     * @author qianchengcheng
     * @date 2024/6/28 15:26
     */
    @GetMapping("backStage/delMaterialPlan")
    fun delMaterialPlan(@RequestParam("ids") ids: List<Int?>?): BaseResp<Any?>? {
        return zhgdMaterialsSupplyPlanService?.delMaterialPlan(ids)
    }

    /**
     * @description 后台管理-进度管理-分页查询物资供应计划
     * @author qianchengcheng
     * @date 2024/6/28 15:39
     */
    @GetMapping("backStage/getMaterialPlanPageInfo")
    fun getMaterialPlanPageInfo(
        @RequestParam(name = "keyWord") keyWord: String?,
        @RequestParam(name = "page", defaultValue = "1") page: Int,
        @RequestParam(name = "size", defaultValue = "15") size: Int
    ): BaseResp<Any?>? {
        return BaseResp.success(zhgdMaterialsSupplyPlanService?.getMaterialPlanPageInfo(keyWord, page, size))
    }

    /**
     * @description 后台管理-进度管理-导出物资供应计划
     * @author qianchengcheng
     * @date 2024/7/3 17:51
     */
    @GetMapping("backStage/exportMaterialPlan")
    fun exportMaterialPlan(response: HttpServletResponse?) {
        try {
            zhgdMaterialsSupplyPlanService?.exportMaterialPlan(response)
        } catch (e: Exception) {
            e.printStackTrace()
//            log.error("exportMaterialPlan==>物资供应计划导出异常", e)
        }
    }

    @PostMapping("editMaterialPlan")
    fun edit(@RequestBody info: ZhgdMaterialsSupplyPlan?): BaseResp<Any?>? {
        zhgdMaterialsSupplyPlanService?.updateById(info)
        return BaseResp.success()
    }

    @PostMapping("delMaterialPlan")
    fun del(@RequestBody info: ZhgdMaterialsSupplyPlan): BaseResp<Any?>? {
        zhgdMaterialsSupplyPlanService?.removeByIds(info.ids)
        return BaseResp.success()
    }

    @PostMapping("totalMaterialPlan")
    fun edit(): BaseResp<Any?>? {
        val count = zhgdMaterialsSupplyPlanService?.count(null)?.toInt()
        val wraZb = QueryWrapper<ZhgdMaterialsSupplyPlan>()
        wraZb.eq("invite_flag", 1)
        val zbCountYes = zhgdMaterialsSupplyPlanService?.count(wraZb)?.toInt()
        val wraDh = QueryWrapper<ZhgdMaterialsSupplyPlan>()
        wraDh.eq("hand_over_materials_flag", 1)
        val dhCountYes = zhgdMaterialsSupplyPlanService?.count(wraZb)?.toInt()
        val out: MutableList<CakeInfo> = ArrayList()
        out.add(CakeInfo(name = "应招标", value = count))
        out.add(CakeInfo(name = "已招标", value = zbCountYes))
        out.add(CakeInfo(name = "已到货", value = dhCountYes))
        return BaseResp.success(out)
    }

}